package com.front.web.lvyou_specialty_front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.common.utils.excel.ExportExcel;
import com.bootdo.common.utils.excel.ExportLvyouExcel;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyOrderAll;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyOrderContent;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyProduct;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/f/productController")
public class ProductFrontController extends LvyouSpecialtySuperController {

    /**
     * 根据分类查看商品
     * http://localhost/f/productController/getProductByType
     * @param typeId 类型id
     * @param priceFlag 0代表升序 1代表降序
     * @param name 商品名称
     * @return
     */
    @RequestMapping("getProductByType")
    public R getProductByType(@RequestParam("typeId")Long typeId,
                              @RequestParam(value = "pageNo", defaultValue = "1")Integer pageNo,
                              @RequestParam(value = "name", defaultValue = "")String name,
                              @RequestParam(value = "priceFlag", defaultValue = "-1")Integer priceFlag,
                              @RequestParam(value = "lowPrice", defaultValue = "")String lowPrice,
                              @RequestParam(value = "highPrice", defaultValue = "")String highPrice,
                              @RequestParam(value = "pageSize", defaultValue = "15")Integer pageSize) {
        Map<String, Object> param = getPageNo(pageNo, pageSize);
        param.put("typeId", typeId);
        param.put("pendingFlag", 3);
        param.put("status", 1);
        param.put("priceFlag", priceFlag);
        param.put("name", name);
        List<LvyouSpecialtyProduct> productFront = lvyouSpecialtyProductService.getByTypeFront(param);
        param.remove("offset");
        param.remove("limit");
        int total = lvyouSpecialtyProductService.count(param);

        return R.ok().put("l", productFront).put("total", total);
    }
    /**
     * 查看单个商品
     * http://localhost/f/productController/getProductDetail
     * @param pid
     * @return
     */
    @RequestMapping("getProductDetail")
    public R getProductDetail(@RequestParam("pid")Long pid) {
        return R.ok().put("i", lvyouSpecialtyProductService.get(pid));
    }

    /**
     * 商品复合查询
     * http://localhost/f/productController/getGoodsBySortAndType
     * @param name 要查询的商品名
     * @param highPrice 最高价
     * @param lowPrice 最低价
     * @param typeId 商品的类型
     * @param priceFlag 1代表升序  2代表降序
     *
     * @return
     */
    @RequestMapping(value = "getGoodsBySortAndType")
    public R getGoodsBySortAndType(@RequestParam(value = "name", defaultValue = "")String name,
                                   @RequestParam(value = "priceFlag", defaultValue = "-1")Integer priceFlag,
                                   @RequestParam(value = "lowPrice", defaultValue = "")String lowPrice,
                                   @RequestParam(value = "highPrice", defaultValue = "")String highPrice,
                                   @RequestParam(value = "typeId", defaultValue = "")String typeId,
                                   @RequestParam(value = "pageNo", defaultValue = "1")Integer pageNo,
                                   @RequestParam(value = "pageSize", defaultValue = "15")Integer pageSize) {
        Map<String, Object> param = new HashMap<>();

        param.put("name", name);
        param.put("priceFlag", priceFlag);

        param.put("lowPrice", lowPrice);
        param.put("highPrice", highPrice);
        param.putAll(getPageNo(pageNo, pageSize));
        param.put("status", 1);

        if (!StringUtils.isEmpty(typeId)) {
            try {
                param.put("typeId", Long.parseLong(typeId));
            } catch (Exception e) {
                return R.error("参数错误");
            }
        }

        List<LvyouSpecialtyProduct> list = lvyouSpecialtyProductService.compoundQuery(param);

        param.remove("offset");
        param.remove("pageSize");
        Integer total = lvyouSpecialtyProductService.compoundQueryCount(param);


        return R.ok().put("l", list).put("total", total);
    }

    @RequestMapping("/exportExcel/content")
    public void exportExcelContent(@RequestParam("query") String query,
                            HttpServletRequest request,
                            HttpServletResponse response){
        JSONObject param = null;
        try {
            param = JSON.parseObject(query);
        } catch (Exception e) {
            String result = JSON.toJSONString(R.error("参数有误"));
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            try {
                response.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }

        if (param.get("limit") != null) {
            param.remove("limit");
        }

        if (param.get("offset") != null) {
            param.remove("offset");
        }

        List<LvyouSpecialtyOrderContent> list = lvyouSpecialtyOrderContentService.list(param);

        Map<String, List<LvyouSpecialtyOrderContent>> dealMap = new HashMap<>();

        for (LvyouSpecialtyOrderContent one: list) {
            String key = one.getMerchantId() + one.getMerchantName();
            if (dealMap.containsKey(key)) {
                List<LvyouSpecialtyOrderContent> contentList = dealMap.get(key);
                contentList.add(one);
            } else {
                List<LvyouSpecialtyOrderContent> listOne = new ArrayList<>();
                listOne.add(one);
                dealMap.put(key, listOne);
            }
        }
        ExportLvyouExcel exportExcel = new ExportLvyouExcel("订单汇总表", LvyouSpecialtyOrderContent.class, dealMap);


        exportExcel.setDataList();
        try {
            exportExcel.write(response, "汇总表.xls");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/exportExcel/orderAll")
    public void exportExcelOrderAll(@RequestParam Map<String, Object> params,
                            HttpServletRequest request,
                            HttpServletResponse response){
        List<LvyouSpecialtyOrderAll> list = lvyouSpecialtyOrderAllService.list(params);
        ExportExcel exportExcel = new ExportExcel("订单汇总表", LvyouSpecialtyOrderContent.class);
        exportExcel.setDataList(list);
        try {
            exportExcel.write(response, "总订单汇总表.xls");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("getLunbo")
    public R getLunbo(@RequestParam(value = "typeId", defaultValue = "0")Long typeId){
        if (typeId == 0) {
            return R.error();
        }

        List<LvyouSpecialtyProduct> lunbo = lvyouSpecialtyProductService.getLunbo(typeId);
        return R.ok().put("l", lunbo);
    }
}
